﻿<%@ WebHandler Language="C#" Class="RbContainerTraceBackHandler" %>


/*Description: 热表批次追溯处理程序
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer: Zhangrj 
'create Date:20240606
'Rewriter:
'Rewrite Date:
*/
using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using System.Text;
using System.Linq;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;
using System.Collections;
using NPOI.SS.UserModel;
using Camstar.WebClient.WebServicesProxy.InSiteWebServices;
using BusinessRule.Common;

public class RbContainerTraceBackHandler : IHttpHandler, IRequiresSessionState
{
    CurrentUserInfo m_UserInfo;
    uLMSCommonBusiness commonBusiness = new uLMSCommonBusiness();
    string fileBasePath = System.Configuration.ConfigurationManager.AppSettings["ImportPath"].ToString();//导入临时路径

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            string action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            if (m_UserInfo == null)
            {
                context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "登录过期，请重新登录" }));
                return;
            }
            switch (action)
            {
                case "GetRbContainerTraceBackList": //获取批次追溯数据（子卡）
                    GetRbContainerTraceBackList(context);
                    break;
                default:
                    context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "未知功能" }));
                    break;
            }
        }
        catch (Exception ex)
        {
            uMESProcessResult result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    /// <summary>
    /// 获取批次追溯数据
    /// </summary>
    /// <param name="context"></param>
    public void GetRbContainerTraceBackList(HttpContext context)
    {
        StringBuilder sql = new StringBuilder();
        sql.AppendLine(@"SELECT rt.*,es.fullname sendEmpName,er.fullname reciveEmpName,ec.fullname completeEmpName,'' filename
                         FROM RbContainerTraceBackInfo rt
                         LEFT JOIN employee es ON es.employeeid = rt.sender
                         LEFT JOIN employee er ON er.employeeid = rt.reciver
                         LEFT JOIN employee ec ON ec.employeeid = rt.completer
                         WHERE rt.status = 0");
        //查询订单号
        if (!string.IsNullOrWhiteSpace(context.Request["mfgOrderName"].ToString()))
        {
            sql.AppendFormat(" AND rt.mfgordername like '%{0}%'", context.Request["mfgOrderName"].ToString());
        }
        //查询件号
        if (!string.IsNullOrWhiteSpace(context.Request["productName"].ToString()))
        {
            sql.AppendFormat(" AND rt.productname like '%{0}%'", context.Request["productName"].ToString());
        }
        //查询子卡
        if (!string.IsNullOrWhiteSpace(context.Request["subContainerName"].ToString()))
        {
            sql.AppendFormat(" AND rt.subcontainername LIKE '%{0}%'", context.Request["subContainerName"].ToString());
        }
        //查询主卡
        if (!string.IsNullOrWhiteSpace(context.Request["mainContainerName"].ToString()))
        {
            sql.AppendFormat(" AND rt.maincontainername like '%{0}%'", context.Request["mainContainerName"].ToString());
        }
        //查询转出人
        if (!string.IsNullOrWhiteSpace(context.Request["sendEmpName"].ToString()))
        {
            sql.AppendFormat(" AND es.fullname like '%{0}%'", context.Request["sendEmpName"].ToString());
        }
        //查询接收人
        if (!string.IsNullOrWhiteSpace(context.Request["receiveEmpName"].ToString()))
        {
            sql.AppendFormat(" AND er.fullname like '%{0}%'", context.Request["receiveEmpName"].ToString());
        }
        //查询完成人
        if (!string.IsNullOrWhiteSpace(context.Request["completeEmpName"].ToString()))
        {
            sql.AppendFormat(" AND ec.fullname like '%{0}%'", context.Request["completeEmpName"].ToString());
        }
        sql.AppendLine(" ORDER BY rt.completetime desc,rt.mfgordername");
        uMESPagingDataDTO pageDto = new uMESPagingDataDTO();
        pageDto.CurrentPageIndex = int.Parse(context.Request["pageIndex"].ToString());
        pageDto.PageSize = int.Parse(context.Request["pageSize"].ToString());
        pageDto.strSQL = sql.ToString();
        uMESProcessResult result = DatabaseHelper.RunProcedureForPage(pageDto);
        foreach (DataRow item in result.DBTable.Rows)
        {
            item["filename"] = item["subcontainername"].ToString() + DateTime.Now.ToString("yyyyMMddHHmmssffff");
        }
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}